import UserAPI from "@/api/user"
import type { IContentConfig } from "@/components/CURD/types"
import { isEmptyVal } from "@/utils/is"
import { useDict } from "@/utils/dict"

const { gender, user_status } = toRefs<any>(useDict("gender", "user_status"))

const contentConfig: IContentConfig = {
  pageName: "user",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  pagination: {
    background: true,
    layout: "prev,pager,next,jumper,total,sizes",
    pageSize: 10,
    pageSizes: [10, 20, 30, 50],
  },
  indexAction: function (params) {
    const queryCondition: QueryCondition = {
      and: [],
      sorts: [{ field: "create_time", sort_asc: false }],
      paging: { current: params.pageNum, size: params.pageSize },
    }
    if (params.createAt) {
      const createAt = params.createAt as string[]
      if (createAt?.length > 1) {
        queryCondition.and?.push({
          field: "create_time",
          op: "between",
          value: createAt[0] + " 00:00:00",
          value2: createAt[1] + " 23:59:59",
        })
      }
      delete params.createAt
    }
    if (!isEmptyVal(params.deptId)) {
      queryCondition.and?.push({ field: "dept_id", op: "=", value: params.deptId })
    }
    if (!isEmptyVal(params.enabled)) {
      queryCondition.and?.push({ field: "enabled", op: "=", value: params.enabled })
    }
    if (!isEmptyVal(params.keywords)) {
      queryCondition.and?.push({
        field: "username",
        op: "like",
        value: "%" + params.keywords + "%",
        or: [
          { field: "nick_name", op: "like", value: "%" + params.keywords + "%" },
          { field: "phone", op: "like", value: "%" + params.keywords + "%" },
        ],
      })
    }
    return UserAPI.getPage(queryCondition)
  },
  deleteAction: UserAPI.deleteByIds,
  importAction(file) {
    return UserAPI.import(1, file)
  },
  exportAction: UserAPI.export,
  importTemplate: UserAPI.downloadTemplate,
  importsAction(data) {
    // 模拟导入数据
    console.log("importsAction", data)
    return Promise.resolve()
  },
  exportsAction: async function (params) {
    // 模拟获取到的是全量数据
    const res = await UserAPI.getPage({})
    console.log("exportsAction", res.data)
    return res.data as any
  },
  pk: "user_id",
  toolbar: ["add", "del"],
  defaultToolbar: ["refresh", "filter", "search"],
  cols: [
    { type: "selection", width: 50, align: "center" },
    { label: "编号", align: "center", prop: "id", width: 100, show: false },
    { label: "用户名", align: "center", prop: "username" },
    { label: "用户昵称xxx", align: "center", prop: "nick_name", width: 120 },
    {
      label: "性别",
      align: "center",
      prop: "gender",
      templet: "dict",
      slotName: "gender",
      options: gender,
      width: 100,
    },
    {
      label: "手机号码",
      align: "center",
      prop: "phone",
      templet: "custom",
      slotName: "phone",
      width: 150,
    },
    {
      label: "状态",
      align: "center",
      prop: "enabled",
      templet: "dict",
      options: user_status,
    },
    {
      label: "创建时间",
      align: "center",
      prop: "create_time",
      "min-width": 150,
    },

    {
      label: "操作",
      align: "center",
      fixed: "right",
      width: 140,
      templet: "tool",
      operat: ["edit", "del"],
    },
  ],
}

export default contentConfig
